package cn.wolfcode.tieba.service.impl;

import cn.wolfcode.tieba.domain.Comment;
import cn.wolfcode.tieba.domain.Tiezi;
import cn.wolfcode.tieba.domain.User;
import cn.wolfcode.tieba.mapper.CommentMapper;
import cn.wolfcode.tieba.service.ICommentService;
import cn.wolfcode.tieba.service.ITieziService;
import cn.wolfcode.tieba.service.IUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author 王金龙
 * @date 2021/2/6 23:25
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {
    @Autowired
    private IUserService userService;
    @Autowired
    private ITieziService tieziService;
    @Override
    public List<Comment> listByTieziId(Long id) {
        List<Comment> comments = super.list(new QueryWrapper<Comment>().eq("tiezi_id", id).isNull("reply_id"));
        for (Comment comment : comments) {
            comment.setAuthor(userService.getById(comment.getAuthorId()));
            comment.setReplyComment(setComments(Collections.singletonList(comment), new ArrayList<>()));
        }
        return comments;
    }
    private List<Comment> setComments(List<Comment> comments, List<Comment> list) {

        // 1.遍历评论
        for (Comment comment : comments) {
            // 2.评论设置作者
            comment.setAuthor(userService.getById(comment.getAuthorId()));
            // 3.根据该评论id查询是否有子评论
            List<Comment> replyComments = super.list(new QueryWrapper<Comment>().eq("reply_id", comment.getId()));
            // 4.没有子评论返回
            if (replyComments.size() == 0) {
                continue;
            }
            // 5.有子评论 子评论设置到评论
            // 层级目录，我不想要层级目录
//            comment.setReplyComment(replyComments);

            list.addAll(replyComments);
            setComments(replyComments, list);
        }
        return list;
    }
    @Override
    public void saveComment(Long id, String content, User user) {
        Comment comment = new Comment();
        comment.setReplyComment(null);
        comment.setTieziId(id);
        Document document = Jsoup.parse(content);
        Elements p = document.getElementsByTag("p");
        String con = p.html();
        comment.setContent(con);
        comment.setAuthor(user);
        comment.setAuthorId(user.getId());
        comment.setCreateTime(new Date());
        super.save(comment);
        // 帖子数中的回复数+1
        tieziService.update(new UpdateWrapper<Tiezi>().setSql("replynum = replynum + 1").eq("id",id));
    }

    @Override
    public void saveComments(Long id, Long commentId, String content, User user) {

        Comment comment = new Comment();
        if ("回复".equals(content.substring(0, 2))) {
            String name = content.split("&nbsp;")[1];
            String con = content.split("&nbsp;")[2].substring(1);
            // 根据内容 查到依赖人
            User replyUser = userService.getByName(name);
            // 根据依赖人和主评论id 查询依赖评论
            List<Comment> replyComments = getReplyId(replyUser.getId(), commentId);
            // 根据内容回复名 和 依赖的主评论 查询出多个依赖评论,取最后一个id

            comment.setReplyId(replyComments.get(replyComments.size() - 1).getId());
            comment.setContent(con);
            comment.setReplyAuthorName(name);
        } else {
            comment.setReplyId(commentId);
            comment.setContent(content);
            // 主评论id查出主评论作者id，再查出作者信息，取出作者名
            Long authorId = super.getById(commentId).getAuthorId();
            String name = userService.getById(authorId).getName();
            comment.setReplyAuthorName(name);
        }

        comment.setAuthorId(user.getId());
        comment.setTieziId(id);
        comment.setCreateTime(new Date());
        comment.setCommentId(commentId);
        super.save(comment);
        // 帖子数中的回复数+1
        tieziService.update(new UpdateWrapper<Tiezi>().setSql("replynum = replynum + 1").eq("id",id));
    }

    @Override
    public List<Comment> getReplyId(Long replyId, Long commentId) {
        return super.list(new QueryWrapper<Comment>().eq("comment_id", commentId).eq("author_id", replyId));
    }


}
